//
//  HANetworkConfig.m
//  HAFund
//
//  Created by 夏成龙 on 2018/11/11.
//  Copyright © 2018年 夏成龙. All rights reserved.
//

#import "HANetworkConfig.h"

#import "UIDevice+Supplement.h"
#import "FactoryClass.h"

NSString *const appenvironmentKey = @"appEvnironmentKey";

@implementation HANetworkConfig

@synthesize environment = _environment;

+ (instancetype)instance {
    static HANetworkConfig *config = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        config = [[HANetworkConfig alloc] init];
        config ->_environment = [[NSUserDefaults standardUserDefaults] integerForKey:appenvironmentKey];
    });
    return config;
}

- (NSString *)baseURL {
#ifdef DEBUG
    if (self.environment == HAAppEnvironmentUAT) {
        return UAT_BASE_URL;
    } else if (self.environment == HAAppEnvironmentSIT) {
        return SIT_BASE_URL;
    } else if (self.environment == HAAppEnvironmentOFF) {
        return OFF_BASE_URL;
    }
    return @"";
#else
    return OFF_BASE_URL;
#endif
}

- (HAAppEnvironment)environment {
#ifdef DEBUG
    return _environment;
#else
     return HAAppEnvironmentOFF
#endif
    return _environment;
}

- (void)setEnvironment:(HAAppEnvironment)environment {
    if (_environment == environment) {
        return;
    }
    _environment = environment;
    [[NSUserDefaults standardUserDefaults] setInteger:environment forKey:appenvironmentKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSDictionary *)commonHeadParams {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    //#设备信息
    NSDictionary *deviceInfo= @{
                                @"osType":@"iOS",
                                @"osVersion":[[UIDevice currentDevice] systemVersion],
                                @"brand":[[UIDevice currentDevice] model],
                                @"appId":[UIDevice getUDID],
                                @"bbVersion":app_Version
                                };
    //格式化
    NSError *parseError = nil;
    NSData *deviceInfoData = [NSJSONSerialization dataWithJSONObject:deviceInfo options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *deviceInfoStr = [[NSString alloc] initWithData:deviceInfoData encoding:NSUTF8StringEncoding];
    deviceInfoStr = [deviceInfoStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    
    return @{ @"channelId":@"000000000000",
              @"tradeMode":@"APP",
              @"senderId":@"1026",
              @"headPar":deviceInfoStr,
             };
}

- (NSDictionary *)extraHeadParams {
    //#登录信息
    NSDate * date = [NSDate date];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyyMMdd.HHmmssSSS";
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    NSString * times = [dateFormatter stringFromDate:date];
    int radom = arc4random() % 1000000;
    NSString *userId = @"";
    NSString *cid = @"";
    NSString *paramsEncryptHead = [NSString stringWithFormat:@"{times=%@,random=%d,userId=%@,cid=%@}", times, radom, userId, cid];
    //格式化和加密/
    NSString *paramStr= [FactoryClass getAesEncrypt:paramsEncryptHead];
    
    dateFormatter.dateFormat = @"yyyyMMdd";
    NSString * dateStr = [dateFormatter stringFromDate:date];
    
    dateFormatter.dateFormat = @"HHmmss";
    NSString * timeStr = [dateFormatter stringFromDate:date];
    
    return @{@"auth":paramStr,
             @"sysDate":dateStr,
             @"sysTime":timeStr,
             };
}

@end
